package com.koron.css2.meterService.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.meterService.bean.MsInfoChangeDetailBean;
import com.koron.css2.meterService.bean.query.MsInfoChangeQueryBean;
import com.koron.css2.meterService.dto.MsInfoChangeDto;
import com.koron.css2.meterService.mapper.MsInfoChangeDetailMapper;
import com.koron.css2.meterService.mapper.MsInfoChangeMapper;
import com.koron.css2.meterService.mapper.MsInfoTempMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 变更列表查询
 * @author bin
 *
 */
public class WmChgList implements ServerInterface{
	
	public static Logger log = LogManager.getLogger(WmChgList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
		try {
			MsInfoChangeQueryBean bean = JsonUtils.objectToPojo(req.getData(), MsInfoChangeQueryBean.class);
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			MsInfoChangeMapper mapper = factory.getMapper(MsInfoChangeMapper.class);
			MsInfoTempMapper msInfoMapper = factory.getMapper(MsInfoTempMapper.class);
			MsInfoChangeDetailMapper detailMapper = factory.getMapper(MsInfoChangeDetailMapper.class);
			List<MsInfoChangeDto> data = mapper.selectList(bean);
			if (CollectionUtils.isNotEmpty(data)){
				List<String> changeIds = data.stream().map(MsInfoChangeDto::getId).collect(Collectors.toList());
				List<MsInfoChangeDetailBean> msInfoChangeDetailBeans = detailMapper.selectListByIds(changeIds);
				Map<String, List<MsInfoChangeDetailBean>> msInfoChangeDetailMap = msInfoChangeDetailBeans.stream().collect(Collectors.groupingBy(MsInfoChangeDetailBean::getChangeId));
				HashMap<String, List<String>> hashMap = new HashMap<>();
				msInfoChangeDetailMap.forEach((k,v)->{
					List<String> userNos = v.stream().map(dto -> msInfoMapper.selectUserNoById(dto.getMsInfoId())).collect(Collectors.toList());
					hashMap.put(k,userNos);
				});
				data.parallelStream().forEach(t -> {
					t.setUserNos(hashMap.get(t.getId()));
					t.setProcessStateName(Tools.transProcessStatus(t.getProcessState()));
				});
			}
			info.setData(new PageInfo<>(data));

		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("查询失败");
			log.error(e.getMessage(), e);
		}
		return info;
	}

}
